

#include "operation.h"
#include "MYALARM.h"


String Msg = "";
int flag = 0;
int alarmSwith = 0;//默认一开始按钮是关闭状态
int doorKey = 0;
int flaglight = 0;//默认灯非光敏状态
int rainSwitch = 0;//默认一开始天晴
int fireSwitch = 0;//默认一开始没火

void operateinit()
{
    LCDSetCursor(0, 0);
    LCDPrint("READY");
}



void changeOperation()
{  
  Msg = "";
  flag = 0;
  while (Serial.available() > 0)
    {
        Msg += char(Serial.read());
        delay(2);
        flag = 1;
    }
    if(flag == 1){
      LCDclearLine1();
      LCDSetCursor(0, 0);
      LCDPrint(Msg);

       if (Msg == "INIT")
       {
          allState();
       }
       
       if (Msg == "DHTRequest")
       {
          tpOperation();
       }
       if (Msg[0] == 'S' && Msg[1] == 'T' && Msg[2] == 'E' && Msg[3] == 'E' && Msg[4] == 'R' && Msg[5] == 'B')
       {
          spanOperation();
       }
       if (Msg[0] == 'S' && Msg[1] == 'A' && Msg[2] == 'F' && Msg[3] == 'E' && Msg[4] == 'T' && Msg[5] == 'Y' && Msg[6] == 'R')//警报点击的返回信号为一个参数1/0（1为开，0为关），前缀为xaa
       {
          alarmSwith = atoi(String(Msg[14]).c_str());
          alarmOperation();
       
          delay(10);
       }
       if (Msg[0] == 'S' && Msg[1] == 'A' && Msg[2] == 'F' && Msg[3] == 'E' && Msg[4] == 'T' && Msg[5] == 'Y' && Msg[6] == 'C')//查询是否有人进入
       {
          if (alarmSwith == 0)
          {
            Serial.println("alarm not open");
          }else
          {
            havePerson();
          }
       }
       
       if (Msg[0] == 'S' && Msg[1] == 'T' && Msg[2] == 'E' && Msg[3] == 'E' && Msg[4] == 'R' && Msg[5] == 'A')
       {
          doorTime();
       }
       if (Msg[0] == 'L' && Msg[1] == 'I' && Msg[2] == 'G' && Msg[3] == 'H' && Msg[4] == 'T')//控制灯的亮度模块
       {
          if(Msg[13] != '-')
          {
            flaglight = 0;
            int lightvalue = atoi( (String(Msg[13]) + String(Msg[14]) + String(Msg[15])).c_str());
            lightControl(lightvalue);
            lightOperation();
          }
          else
          {
            flaglight = 1;
            light();
            lightSensorOp();
          }
          
       }
       if (Msg == "PRESSURERequest")//输入指令来显示窗户状态，一个参数0，前缀xwa
       {
          windowShow();
       }
       if (Msg == "REEDRequest")//输入指令来显示门的状态，一个参数0，前缀xea
       {
          doorShow();
       }
       if (Msg == "RAINRequest")//输入指令显示是否下雨
       {
          if(waterSensor()<500)
          {
            rainSwitch = 1;
          }else
          {
            rainSwitch = 0;
          }
          haveRain();
       }
       if (Msg == "FIRERequest")//输入指令显示是否下雨
       {
          if(fireSensor() < 500)
          {
            fireSwitch = 1;
          }else
          {
            fireSwitch = 0;
          }
          haveFire();
       }
       
       }
       
}

void alarmOperation()
{
  String alarmstate;
  String statu;
  if(alarmSwith == 0){
      alarmstate = "alarm: close";
      statu = "SAFTTY status 0";
      LCDclearLine2();
      LCDSetCursor(0, 1);
      LCDPrint(alarmstate);
  }else{
      alarmstate = "alarm: open";
      statu = "SAFTTY status 1";
      LCDclearLine2();
      LCDSetCursor(0, 1);
      LCDPrint(alarmstate);
  }
  Serial.println(statu);
}

void havePerson()
{
  int person = sentVal();
  if(person > 600)
  {
    Serial.println("have person enter");
  }else
  {
    Serial.println("no one enter");
  }
}


void haveRain()
{
  String showrain;
  if(rainSwitch == 1)
  {
    Serial.println("RAIN status 1");
    showrain = "have rain";
    LCDclearLine2();
    LCDSetCursor(0, 1);
    LCDPrint(showrain);
  }else
  {
    Serial.println("RAIN status 0");
    showrain = "no rain";
    LCDclearLine2();
    LCDSetCursor(0, 1);
    LCDPrint(showrain);
  }
}

void haveFire()
{
  String showfire;
  if(fireSwitch == 1)
  {
    Serial.println("FIRE status 1");
    showfire = "have fire";
    LCDclearLine2();
    LCDSetCursor(0, 1);
    LCDPrint(showfire);
  }else
  {
    Serial.println("FIRE status 0");
    showfire = "no fire";
    LCDclearLine2();
    LCDSetCursor(0, 1);
    LCDPrint(showfire);
  }
}


void doorShow()
{
    
    int doorout = doorState();
    String showdoor;
    String statu;
    if(doorout == 0){
      showdoor = "door: close";
      statu = "REED status 0";
      LCDclearLine2();
      LCDSetCursor(0, 1);
      LCDPrint(showdoor);
    }else{
      showdoor = "door: open";
      statu = "REED status 1";
      LCDclearLine2();
      LCDSetCursor(0, 1);
      LCDPrint(showdoor);
    }
    Serial.println(statu);
}

void windowShow()
{
    
    int windowout = windowState();
    String showwindow;
    String statu;
    if(windowout == 0){
     showwindow = "window: close";
     statu = "PRESSURE status 0";
     LCDclearLine2();
     LCDSetCursor(0, 1);
     LCDPrint(showwindow);
    }else{
     showwindow = "window: open";
     statu = "PRESSURE status 1";
     LCDclearLine2();
     LCDSetCursor(0, 1);
     LCDPrint(showwindow);
    }
    Serial.println(statu);
}

void lightSensorOp()
{
     String lig = "LIGHT status:self";
     LCDclearLine2();
     LCDSetCursor(0, 1);
     LCDPrint(lig);
     Serial.println(lig);
}

void lightOperation()
{
     String lig = "LIGHT status " + String(lightVal());
     LCDclearLine2();
     LCDSetCursor(0, 1);
     LCDPrint(lig);
     Serial.println(lig);
}


void tpOperation()
{
     String result = "temp=" + String(readTemperature()) + "&" + "hum=" + String(readHumidity());
     LCDclearLine2();
     LCDSetCursor(0, 1);
     LCDPrint(result);
     Serial.println(result);
}

void spanOperation()
{
      int angle = atoi( (String(Msg[14]) + String(Msg[15])).c_str());
      doorMove(angle);
      String result = String("Window spin") + String(angle);
      String statu = "STEERTBangle " + String(angle);
      LCDclearLine2();
      LCDSetCursor(0, 1);
      LCDPrint(result);
      Serial.println(statu);
}


void alarmUse()
{
    if(alarmSwith == 1){
          useAlarm();
       }else{
          closeAlarm();
       }
}

void doorTime()
{    
     int doorState = autoDoor(Msg[14]);
     String result;
     if(doorState == 1){
     result = String("Door open") ;
     }
     else{
      result = String("Door close") ;
     }
     String statu = "STRRTAstatus " + String(doorState);
     LCDclearLine2();
     LCDSetCursor(0, 1);
     LCDPrint(result);
     Serial.println(statu);
}



void allState()
{
    String tandh = "Temp:" + String(readTemperature()) + "C;" + "Hum:" + String(readHumidity()) + "%";
    String alarmstate;
    String showdoor;
    String showwindow;
    int doorout = doorState();
    int windowout = windowState();
    
    if(windowout == 0){
     showwindow = "window: close";
    }else{
     showwindow = "window: open";
    }
    
    if(alarmSwith == 0){
      alarmstate = "alarm: close";
  }else{
      alarmstate = "alarm: open";
  }

    if(doorout == 0){
      showdoor = "door: close";
    }else{
      showdoor = "door: open";
    }

    Serial.print(tandh + "?");
    Serial.print(alarmstate + "?");
    Serial.print(showdoor + "?");
    Serial.print(showwindow + "?");
}
